package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.controller.GoodsController.GoodsDetail.SubGoodsResponse
import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.repository.QueryGoodsUnitResponse
import com.zxy.supplier_system.server.service.MarketOrderWithProcurementConvertService
import com.zxy.supplier_system.server.service.ProcurementReceiptService
import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.utils.LongIdAndName
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.InputStreamResource
import org.springframework.core.io.Resource
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.OffsetDateTime


@RestController
@RequestMapping("procurementSuppliers/receipts")
class ProcurementReceiptController(
    private val procurementReceiptService: ProcurementReceiptService,
    @Value("classpath:excel/templates/导入采购入库货品模板.xlsx")
    private val importItemsTemplate: Resource,
    private val marketOrderWithProcurementConvertService: MarketOrderWithProcurementConvertService
) {

    data class QueryProcurementReceiptRequest(
        val supplierIds: List<Int> = emptyList(),
        val startCreatedDate: LocalDate?,
        val endCreatedDate: LocalDate?,
        val validatedStartDate: LocalDate?,
        val validatedEndDate: LocalDate?,
    )

    @HasAuthority(Authority.采购_入库_查询)
    @GetMapping
    fun queryProcurementReceipts(
        request: QueryProcurementReceiptRequest,
        @PageableDefault(sort = ["createdDateTime"], direction = Sort.Direction.DESC) pageable: Pageable
    ): Page<QueryProcurementReceiptResponse> {
        return this.procurementReceiptService.queryProcurementReceipts(request, pageable)
    }

    data class QueryProcurementReceiptResponse(
        val id: Int,
        val validatedDateTime: OffsetDateTime?,
        val createdDateTime: OffsetDateTime,
        val supplier: IntIdAndName,
        val goodsKindAmount: Int,
        val procurementCreatedDateTime: OffsetDateTime,
    )

    @HasAuthority(Authority.采购_入库_查询)
    @GetMapping("{id}")
    fun getProcurementReceiptDetail(
        @PathVariable id: Int,
        includeItems: Boolean = false,
    ): ProcurementReceiptDetail {
        return this.procurementReceiptService.getProcurementReceiptDetail(id, includeItems)
    }

    @HasAuthority(Authority.采购_入库_删除)
    @DeleteMapping("{id}")
    fun deleteProcurementReceipt(
        @PathVariable id: Int,
    ) {
        return this.procurementReceiptService.delete(id)
    }


    data class ProcurementReceiptDetail(
        val id: Int,
        val createdDateTime: OffsetDateTime,
        val supplier: IntIdAndName,
        val items: List<ProcurementReceiptItemResponse>,
        val validatedDateTime: OffsetDateTime?,
        val procurementId: Int?,
    )

    data class ProcurementReceiptItemResponse(
        val id: Int,
        val name: String,
        val barcode: String,
        val price: BigDecimal,
        val amount: Int,
        val brand: IntIdAndName?,
        val category: IntIdAndName?,
        val factory: IntIdAndName?,
        val amountInOrder: Int,
        val boxSpecification: String?,
        val unit: LongIdAndName?,
        val units: List<QueryGoodsUnitResponse>,
        val subGoods: SubGoodsResponse?,
        val note: String?,
        val practicalAmount: Int?,
        val purchasePrice: BigDecimal,
    )

    @HasAuthority(Authority.采购_入库_验收)
    @PatchMapping("{id}/validate")
    fun validate(@PathVariable id: Int, itemDefaultValidated: Boolean = false) {
        this.procurementReceiptService.validate(id, itemDefaultValidated)
    }

    data class UpdateProcurementReceiptNoteRequest(
        val note: String,
    )

    @HasAuthority(Authority.采购_入库_修改)
    @PatchMapping("{id}/note")
    fun updateProcurementNote(@PathVariable id: Int, @RequestBody request: UpdateProcurementReceiptNoteRequest) {
        this.procurementReceiptService.updateProcurementReceiptNote(id, request)
    }

    @HasAuthority(Authority.采购_入库_修改)
    @GetMapping("items/excel/import/template")
    fun downloadImportTemplate(): InputStreamResource {
        return InputStreamResource(importItemsTemplate.inputStream)
    }

    @HasAuthority(Authority.卖场_订单_转化为送货单)
    @PostMapping("{receiptId}/convertToMarketDeliveryOrder")
    fun convertToMarketDeliveryOrder(@PathVariable receiptId: Int,
                                marketOrderNumber: String) : Int{
        return marketOrderWithProcurementConvertService.convertToMarketDelivery(receiptId,marketOrderNumber).id
    }

}
